package com.pac.weixin.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.weixin.bean.WxDoctorSelBean;
import com.pac.weixin.dao.WxDoctorSelDao;
import com.pac.weixin.service.WxDoctorSelService;

/**
 * 微信用户选择医生service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月28日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "wxDoctorSelService")
public class WxDoctorSelServiceImpl implements WxDoctorSelService {

	private static Logger logger = Logger
			.getLogger(WxDoctorSelServiceImpl.class);

	@Autowired
	private WxDoctorSelDao wxDoctorSelDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addWxDoctorSel(WxDoctorSelBean wxDoctorSelBean) {
		if (null == wxDoctorSelBean) {
			logger.error("添加微信选择医生记录出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxDoctorSelBean.getOpenId())) {
			logger.error("添加微信选择医生记录出错，微信openid为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "微信openId为空");
		}

		if (StringTools.isNull(wxDoctorSelBean.getUserId())) {
			logger.error("添加微信选择医生记录出错，医生用户Id为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医生用户ID为空");
		}

		// 删除微信用户原来选择的医生
		WxDoctorSelBean wxDoctorSelBeanD = new WxDoctorSelBean();
		wxDoctorSelBeanD.setOpenId(wxDoctorSelBean.getOpenId());
		delWxDoctorSel(wxDoctorSelBeanD);

		// 添加纪录
		return wxDoctorSelDao.addWxDoctorSel(wxDoctorSelBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delWxDoctorSel(WxDoctorSelBean wxDoctorSelBean) {
		if (null == wxDoctorSelBean) {
			logger.error("删除微信聊天记录出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxDoctorSelBean.getOpenId())) {
			logger.error("山微信聊天记录出错，微信openid为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "微信openId为空");
		}

		return wxDoctorSelDao.delWxDoctorSel(wxDoctorSelBean);
	}

	@Override
	public List<WxDoctorSelBean> queryWxDoctorSel(
			WxDoctorSelBean wxDoctorSelBean) {
		return wxDoctorSelDao.queryWxDoctorSel(wxDoctorSelBean);
	}

	@Override
	public WxDoctorSelBean findWxDoctorSel(WxDoctorSelBean wxDoctorSelBean) {
		if (null == wxDoctorSelBean) {
			return null;
		}

		List<WxDoctorSelBean> doctorSelList = wxDoctorSelDao
				.queryWxDoctorSel(wxDoctorSelBean);
		if (null != doctorSelList && doctorSelList.size() > 0) {
			return doctorSelList.get(0);
		}
		return null;
	}

	@Override
	public WxDoctorSelBean findWxDoctorSelByOpenid(String openid) {
		if (StringTools.isNull(openid)) {
			return null;
		}

		WxDoctorSelBean wxDoctorSelBean = new WxDoctorSelBean();
		wxDoctorSelBean.setOpenId(openid);
		List<WxDoctorSelBean> doctorSelList = wxDoctorSelDao
				.queryWxDoctorSel(wxDoctorSelBean);
		if (null != doctorSelList && doctorSelList.size() > 0) {
			return doctorSelList.get(0);
		}

		return null;
	}

}
